#include "Galaxy.h"

Galaxy::Galaxy( ) 
{
	//create quadrants
	for( int i = 0; i < 8; i++ )
		for( int j = 0; j < 8 ; j++ )
			m_Quad[i][j] = new Quadrant( ) ;

	//create player
	m_player = new Player( ) ;

	//set the quadrant to identify the player's existence
	m_Quad[m_player->GetQuadX()][m_player->GetQuadY()]->PlayerExist( ) ;
}

Galaxy::~Galaxy( )
{
}

void Galaxy::UpdatePlayer( int x, int y )
{
	Quadrant* currQuadrant = m_Quad[m_player->GetQuadX()][m_player->GetQuadY()] ;
	//Player is doing nothing
	if( m_player->GetState( ) == IDLE )
	{
		//if the sector selected by the player input is EMPTY
		if( currQuadrant->GetSector( x, y ) == EMPTY )
		{
			//Move the player and set the target to the sector
			m_player->SetState( MOVE ) ;
			m_player->SetTarget( x, y ) ;
		}
		//if the sector selected by the player input is an ENEMY
		else if(currQuadrant->GetSector( x, y ) == ENEMY )
		{
			//Fire a missile (if available
			if( m_player->GetMissileCount() != 0 )
			{
				m_player->SetState( ATTACK ) ;
				m_player->FireMissile( x, y ) ;

				//Server displays status
				cout << "Player firing missile at enemy in sector " << x << ", " << y << endl ;
			}
			else
				//Server displays status
				cout << "Player tried to attack enemy, but is out of missiles" << endl ;
		}
	}
	//If the player is currently moving
	else if( m_player->GetState( ) == MOVE )
	{
		//move player some more
		m_player->Move( currQuadrant ) ;

		//if he has collided with something or has reached its destination, end the movement
		if( m_player->Collision( currQuadrant ) || HasMoved( ) )
			m_player->SetState( IDLE ) ;
	}
	//if the player is attacking with a missile
	else if( m_player->GetState( ) == ATTACK )
	{
		Missile* tempMissile = m_player->GetMissile( ) ;

		//move the missile some more
		m_player->MoveMissile( currQuadrant ) ;

		if( tempMissile->ReachedTarget( ) || tempMissile->CollisionEnemy( currQuadrant ) )
		{
			m_Quad[m_player->GetQuadX()][m_player->GetQuadY()]->DestroyEnemy( true, tempMissile->GetDistance( ), tempMissile->GetPosX(), tempMissile->GetPosY() ) ;
			m_player->SetState( IDLE ) ;
			m_player->DestroyMissile( ) ;
		}
		else if( tempMissile->CollisionStar( currQuadrant ) )
		{
			m_player->SetState( IDLE ) ;
			m_player->DestroyMissile( ) ;
		}
	}
	else
	{
		m_player->Dock( ) ;
		m_player->SetState( IDLE ) ;
	}

}

void Galaxy::WarpPlayer( int x, int y ) 
{
	//tell the last quadrant player is leaving
	m_Quad[m_player->GetQuadX()][m_player->GetQuadY()]->PlayerExist( ) ;

	//warp to sector and server displays status
	m_player->Warp( x, y ) ;
	cout << "Player has warped to quadrant " << x << ", " << y << endl ;

	//let new quadrant know player has arrived
	m_Quad[x][y]->PlayerExist( ) ;
}

void Galaxy::SetBlasters( int energy )
{
	//reduce player's energy by the amount used
	m_player->FireBlasters( energy ) ;

	//damage all enemies in the current quadrant based on energy amount and distance from player's sector
	m_Quad[m_player->GetQuadX()][m_player->GetQuadY()]->DamageEnemies( energy, m_player->GetPosX(), m_player->GetPosY() ) ;
}

void Galaxy::ScanGalaxy( )
{
	if( m_player->GetQuadX() > 0 )
	{
		if( m_player->GetQuadY( ) > 0 )
			if( !m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY() - 1]->getExplored( ) )
				m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY() - 1]->setExplored( true ) ;

		if( !m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY()]->getExplored( ) )
			m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY()]->setExplored( true ) ;

		if( m_player->GetQuadY( ) < 7 )
			if( !m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY() + 1]->getExplored( ) )
				m_Quad[m_player->GetQuadX() - 1][m_player->GetQuadY() + 1]->setExplored( true ) ;
	}

	if( m_player->GetQuadY( ) > 0 )
		if( !m_Quad[m_player->GetQuadX()][m_player->GetQuadY() - 1]->getExplored( ) )
			m_Quad[m_player->GetQuadX()][m_player->GetQuadY() - 1]->setExplored( true ) ;

	if( m_player->GetQuadY( ) < 7 )
		if( !m_Quad[m_player->GetQuadX()][m_player->GetQuadY() + 1]->getExplored( ) )
			m_Quad[m_player->GetQuadX()][m_player->GetQuadY() + 1]->setExplored( true ) ;

	if( m_player->GetQuadX() > 0 )
	{
		if( m_player->GetQuadY( ) > 0 )
			if( !m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY() - 1]->getExplored( ) )
				m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY() - 1]->setExplored( true ) ;

		if( !m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY()]->getExplored( ) )
			m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY()]->setExplored( true ) ;

		if( m_player->GetQuadY( ) < 7 )
			if( !m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY() + 1]->getExplored( ) )
				m_Quad[m_player->GetQuadX() + 1][m_player->GetQuadY() + 1]->setExplored( true ) ;
	}

}

void Galaxy::EnemyTurn( )
{
	m_Quad[m_player->GetQuadX()][m_player->GetQuadY()]->UpdateEnemy() ;
}